﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using XYW.DBUtility;//Please add references
namespace XYW.DAL
{
	/// <summary>
	/// 数据访问类:Images
	/// </summary>
	public partial class Images
	{
		public Images()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("ID", "Images"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Images");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(XYW.Model.Images model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Images(");
			strSql.Append("belongType,belongId,src,thumb,fileName,description,showWhere,AlbumsId,userId,userName,uploadTime)");
			strSql.Append(" values (");
			strSql.Append("@belongType,@belongId,@src,@thumb,@fileName,@description,@showWhere,@AlbumsId,@userId,@userName,@uploadTime)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@belongType", SqlDbType.VarChar,50),
					new SqlParameter("@belongId", SqlDbType.Int,4),
					new SqlParameter("@src", SqlDbType.VarChar,150),
					new SqlParameter("@thumb", SqlDbType.VarChar,150),
					new SqlParameter("@fileName", SqlDbType.VarChar,250),
					new SqlParameter("@description", SqlDbType.VarChar,150),
					new SqlParameter("@showWhere", SqlDbType.VarChar,50),
					new SqlParameter("@AlbumsId", SqlDbType.Int,4),
					new SqlParameter("@userId", SqlDbType.Int,4),
					new SqlParameter("@userName", SqlDbType.VarChar,50),
					new SqlParameter("@uploadTime", SqlDbType.DateTime)};
			parameters[0].Value = model.belongType;
			parameters[1].Value = model.belongId;
			parameters[2].Value = model.src;
			parameters[3].Value = model.thumb;
			parameters[4].Value = model.fileName;
			parameters[5].Value = model.description;
			parameters[6].Value = model.showWhere;
			parameters[7].Value = model.AlbumsId;
			parameters[8].Value = model.userId;
			parameters[9].Value = model.userName;
			parameters[10].Value = model.uploadTime;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(XYW.Model.Images model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Images set ");
			strSql.Append("belongType=@belongType,");
			strSql.Append("belongId=@belongId,");
			strSql.Append("src=@src,");
			strSql.Append("thumb=@thumb,");
			strSql.Append("fileName=@fileName,");
			strSql.Append("description=@description,");
			strSql.Append("showWhere=@showWhere,");
			strSql.Append("AlbumsId=@AlbumsId,");
			strSql.Append("userId=@userId,");
			strSql.Append("userName=@userName,");
			strSql.Append("uploadTime=@uploadTime");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@belongType", SqlDbType.VarChar,50),
					new SqlParameter("@belongId", SqlDbType.Int,4),
					new SqlParameter("@src", SqlDbType.VarChar,150),
					new SqlParameter("@thumb", SqlDbType.VarChar,150),
					new SqlParameter("@fileName", SqlDbType.VarChar,250),
					new SqlParameter("@description", SqlDbType.VarChar,150),
					new SqlParameter("@showWhere", SqlDbType.VarChar,50),
					new SqlParameter("@AlbumsId", SqlDbType.Int,4),
					new SqlParameter("@userId", SqlDbType.Int,4),
					new SqlParameter("@userName", SqlDbType.VarChar,50),
					new SqlParameter("@uploadTime", SqlDbType.DateTime),
					new SqlParameter("@ID", SqlDbType.Int,4)};
			parameters[0].Value = model.belongType;
			parameters[1].Value = model.belongId;
			parameters[2].Value = model.src;
			parameters[3].Value = model.thumb;
			parameters[4].Value = model.fileName;
			parameters[5].Value = model.description;
			parameters[6].Value = model.showWhere;
			parameters[7].Value = model.AlbumsId;
			parameters[8].Value = model.userId;
			parameters[9].Value = model.userName;
			parameters[10].Value = model.uploadTime;
			parameters[11].Value = model.ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Images ");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Images ");
			strSql.Append(" where ID in ("+IDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public XYW.Model.Images GetModel(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 ID,belongType,belongId,src,thumb,fileName,description,showWhere,AlbumsId,userId,userName,uploadTime from Images ");
			strSql.Append(" where ID=@ID");
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;

			XYW.Model.Images model=new XYW.Model.Images();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["ID"]!=null && ds.Tables[0].Rows[0]["ID"].ToString()!="")
				{
					model.ID=int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["belongType"]!=null && ds.Tables[0].Rows[0]["belongType"].ToString()!="")
				{
					model.belongType=ds.Tables[0].Rows[0]["belongType"].ToString();
				}
				if(ds.Tables[0].Rows[0]["belongId"]!=null && ds.Tables[0].Rows[0]["belongId"].ToString()!="")
				{
					model.belongId=int.Parse(ds.Tables[0].Rows[0]["belongId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["src"]!=null && ds.Tables[0].Rows[0]["src"].ToString()!="")
				{
					model.src=ds.Tables[0].Rows[0]["src"].ToString();
				}
				if(ds.Tables[0].Rows[0]["thumb"]!=null && ds.Tables[0].Rows[0]["thumb"].ToString()!="")
				{
					model.thumb=ds.Tables[0].Rows[0]["thumb"].ToString();
				}
				if(ds.Tables[0].Rows[0]["fileName"]!=null && ds.Tables[0].Rows[0]["fileName"].ToString()!="")
				{
					model.fileName=ds.Tables[0].Rows[0]["fileName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["description"]!=null && ds.Tables[0].Rows[0]["description"].ToString()!="")
				{
					model.description=ds.Tables[0].Rows[0]["description"].ToString();
				}
				if(ds.Tables[0].Rows[0]["showWhere"]!=null && ds.Tables[0].Rows[0]["showWhere"].ToString()!="")
				{
					model.showWhere=ds.Tables[0].Rows[0]["showWhere"].ToString();
				}
				if(ds.Tables[0].Rows[0]["AlbumsId"]!=null && ds.Tables[0].Rows[0]["AlbumsId"].ToString()!="")
				{
					model.AlbumsId=int.Parse(ds.Tables[0].Rows[0]["AlbumsId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["userId"]!=null && ds.Tables[0].Rows[0]["userId"].ToString()!="")
				{
					model.userId=int.Parse(ds.Tables[0].Rows[0]["userId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["userName"]!=null && ds.Tables[0].Rows[0]["userName"].ToString()!="")
				{
					model.userName=ds.Tables[0].Rows[0]["userName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["uploadTime"]!=null && ds.Tables[0].Rows[0]["uploadTime"].ToString()!="")
				{
					model.uploadTime=DateTime.Parse(ds.Tables[0].Rows[0]["uploadTime"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,belongType,belongId,src,thumb,fileName,description,showWhere,AlbumsId,userId,userName,uploadTime ");
			strSql.Append(" FROM Images ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" ID,belongType,belongId,src,thumb,fileName,description,showWhere,AlbumsId,userId,userName,uploadTime ");
			strSql.Append(" FROM Images ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Images ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.ID desc");
			}
			strSql.Append(")AS Row, T.*  from Images T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Images";
			parameters[1].Value = "ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

